<template>
	<div class="content">
		<div class="content-header">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>移动管理</el-breadcrumb-item>
				<el-breadcrumb-item>
					<b>版本维护</b>
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="content-body">
			<el-scrollbar>
				<div class="panel">
					<el-form :model="appVersionSearchForm" ref="appVersionSearchForm" inline label-width="100px">
						<el-row>
							<el-col :md="8">
								<el-form-item label="版本名称">
									<el-input v-model="appVersionSearchForm.versionName"></el-input>
								</el-form-item>
							</el-col>
							<el-col :md="8">
								<el-form-item label="强制更新">
									<el-select v-model="appVersionSearchForm.forceUpdate" placeholder="全部">
										<el-option v-for="item in forceUpdateList" :key="item.id" :label="item.name"
											:value="item.id"></el-option>
									</el-select>
								</el-form-item>
							</el-col>
							<el-col :md="8">
								<el-form-item label="类型">
									<el-select v-model="appVersionSearchForm.appType" placeholder="全部">
										<el-option v-for="item in appTypeList" :key="item.id" :label="item.name"
											:value="item.id"></el-option>
									</el-select>
								</el-form-item>
							</el-col>
						</el-row>
						<el-row>
							<el-col :md="8">
								<el-form-item label="发布时间">
									<div style="display: flex; justify-content: space-between; justify-items: center;">
										<el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd"
											v-model="appVersionSearchForm.releaseTimeStart" style="width:130px;">
										</el-date-picker>
										<div style="margin: 0 5px;"> ~ </div>
										<el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd"
											v-model="appVersionSearchForm.releaseTimeEnd" style="width:130px;">
										</el-date-picker>
									</div>
								</el-form-item>
							</el-col>
						</el-row>
						<div class="search-btn">
							<div class="search-btn-left">
								<el-button type="success" icon="el-icon-plus" @click="handleAdd">新增版本</el-button>
							</div>
							<div class="search-btn-right">
								<el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
								<el-button icon="el-icon-refresh" @click="resetForm">重置</el-button>
							</div>
						</div>
					</el-form>
					<el-table :data="tableData" border stripe style="width: 100%">
						<el-table-column label="序号" width="60" header-align="left" align="center">
							<template
								slot-scope="scope">{{scope.$index + 1 + (tableCurrentPage-1)*tablePageSize}}</template>
						</el-table-column>
						<el-table-column label="版本号" prop="versionCode"></el-table-column>
						<el-table-column label="版本名称" prop="versionName"></el-table-column>
						<el-table-column label="类型" prop="appType">
							<template slot-scope="scope">
								<span v-if="scope.row.appType == 'android'">Android</span>
								<span v-if="scope.row.appType == 'ios'">IOS</span>
							</template>
						</el-table-column>
						<el-table-column label="强制更新" prop="forceUpdate">
							<template slot-scope="scope">
								<span v-if="scope.row.forceUpdate == 'Y'">是</span>
								<span v-else>否</span>
							</template>
						</el-table-column>
						<el-table-column label="下载地址" prop="downloadUrl"></el-table-column>
						<el-table-column label="更新说明" prop="updateDesc"></el-table-column>
						<el-table-column label="发布时间" prop="releaseTime"></el-table-column>
						<el-table-column label="操作" min-width="100px">
							<template slot-scope="scope">
								<el-button type="warning" @click="handleUpdate(scope.row.id)">编辑</el-button>
								<el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
							</template>
						</el-table-column>
					</el-table>
					<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
						:current-page.sync="tableCurrentPage" :page-sizes="[10, 20, 30, 50]"
						:page-size.sync="tablePageSize" layout="total, jumper, prev, pager, next, sizes"
						:total="tableTotal"></el-pagination>
				</div>
			</el-scrollbar>
			<AppVersion ref="appVersion"></AppVersion>
		</div>
	</div>
</template>

<script>
	import AppVersion from "./AppVersion";
	export default {
		components: {
			AppVersion
		},
		data() {
			return {
				appVersionSearchForm: {
					versionName: null,
					forceUpdate: null,
					appType: null,
					releaseTimeStart: null,
					releaseTimeEnd: null
				},
				forceUpdateList: [
					{
						id: "Y",
						name: "是"
					},
					{
						id: "N",
						name: "否"
					}
				],
				appTypeList: [
					{
						id: "android",
						name: "Android"
					},
					{
						id: "ios",
						name: "IOS"
					}
				],
				tableTotal: 0,
				tableCurrentPage: 1,
				tablePageSize: 10,
				tableData: []
			};
		},
		mounted() {
			this.getTableData(this.tableCurrentPage, this.tablePageSize);
		},
		methods: {
			handleSizeChange(val) {
				this.getTableData(this.tableCurrentPage, val);
			},
			handleCurrentChange(val) {
				this.getTableData(val, this.tablePageSize);
			},
			handleSearch() {
				this.getTableData(this.tableCurrentPage, this.tablePageSize);
			},
			getTableData(page, pageSize) {

				let pageParams = '?page=' + page + '&rows=' + pageSize;

				this.$api.post('/appVersion/findByPage' + pageParams, this.appVersionSearchForm).then(
					response => {
						this.tableData = response.data.list;
						this.tableTotal = response.data.total;
					}
				);
			},
			resetForm() {
				this.appVersionSearchForm = {
					versionName: null,
					forceUpdate: null,
					appType: null,
					releaseTimeStart: null,
					releaseTimeEnd: null
				};
				this.getTableData(1, this.tablePageSize);
			},
			handleAdd() {
				this.$refs.appVersion.initForm();
			},
			handleUpdate(id) {
				this.$refs.appVersion.initForm(id);
			},
			handleDelete(id) {
				this.$confirm("确认删除?", "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(() => {
					this.$api.post('/appVersion/delete', {
						id: id
					}).then(response => {
						if (response.success) {
							this.$message.success(response.message);
							this.handleSearch();
						} else {
							this.$message.error(response.message);
						}
					});
				}).catch(() => {});
			}
		}
	};
</script>
